package com.xxxt.utils;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.xxxt.entity.Summarize;


public class CreateWord {

	public static String createSummarizeWord(Summarize summarize,String tdirectory,
			HttpServletRequest request,HttpServletResponse response) { 
        String content = 
        		"<p style=\"text-align: center;\"><font size=\"6\"><span style=\"font-family: 宋体;\">"
        		+ summarize.getTitle()
        		+ "<br></span></font></p>"
        		+ "<p style=\"text-align: center;\"><font size=\"5\"><span style=\"font-family: 宋体;\"><font size=\"3\">姓名："
        		+ summarize.getStudent().getName()
        		+ "&nbsp;&nbsp;&nbsp; 学号："
        		+ summarize.getStudent().getSno()
        		+ "&nbsp;&nbsp;&nbsp; 类型："
        		+ summarize.getSummarizeType()
        		+ "&nbsp;&nbsp;&nbsp; 时间："
        		+ summarize.getCommitTime()
        		+ "</font><br></span></font></p>"
        		+ "<p style=\"text-align: left;\"><font size=\"4\"><span style=\"font-family: 宋体;\">正文：</span></font></p>"
        		+ "<p style=\"text-align: left;\"><font size=\"4\"><span style=\"font-family: 宋体;\">&nbsp;&nbsp;&nbsp;"
        		+ summarize.getContent()
        		+ "<br></span></font></p>";
        //存储docx文档的目录名
//        String docxDirectry = "docxDirectry";
//        //判断存储docx文档的目录存在
//        boolean b = FileUploadUtil.isNotFile(request, docxDirectry);
//        if(!b) {
//        	FileUploadUtil.createDirectory(request, response, docxDirectry);
//        }
        try {
        	String fileName = request.getServletContext().getRealPath("/")+"/"+tdirectory+"/"+ summarize.getStudent().getSno() + "_" + summarize.getStudent().getName() + ".docx";
            InputStream is = new ByteArrayInputStream(content.getBytes("GBK")); 
            OutputStream os = new FileOutputStream(fileName);
            POIFSFileSystem fs = new POIFSFileSystem();  
            fs.createDocument(is, "WordDocument");  
            fs.writeFilesystem(os);  
            os.close();
            is.close();
            return fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
		return null;  
    }
}
